package com.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.health.constant.RedisConstant;
import com.health.mapper.SetmealMapper;
import com.health.pojo.Setmeal;
import com.health.service.SetmealService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private FreeMarkerConfig freeMarkerConfig;
    private String outPutPath="D:/health_parent/health_mobile/src/main/resources/static/pages";

    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealMapper.add(setmeal);
        Integer setmealId=setmeal.getId();
        this.setSetmealAndCheckgroup(setmealId,checkgroupIds);
        String fileName=setmeal.getImg();
        redisTemplate.boundSetOps(RedisConstant.SETMEAL_PIC_DB_RESOURCES).add(fileName);
        generateMobileStaticHtml();
    }

    public void generateMobileStaticHtml(){
        List<Setmeal> list=this.list();
        generateMobileSetmealListHtml(list);
        generateMobileSetmealDetailHtml(list);
    }

    public void generateMobileSetmealListHtml(List<Setmeal> list){
        Map map=new HashMap();
        map.put("setmealList",list);
        generteHtml("mobile_setmeal.ftl","m_setmeal.html",map);
    }

    public void generateMobileSetmealDetailHtml(List<Setmeal> list){
        for(Setmeal setmeal:list){
            Map map=new HashMap();
            map.put("setmeal",setmealMapper.findById(setmeal.getId()));
            generteHtml("mobile_setmeal_detail.ftl","setmeal_detail_"+setmeal.getId()+".html",map);
        }
    }

    public void generteHtml(String templateName,String htmlPageName,Map map){
        Configuration configuration=freeMarkerConfig.getConfiguration();
        Writer out=null;
        try {
            Template template=configuration.getTemplate(templateName);
            out=new FileWriter(new File(outPutPath+"/"+htmlPageName));
            template.process(map,out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Page<Setmeal> pageQuery(Integer pagenum, Integer pagesize, String pageQuery) {
        QueryWrapper<Setmeal> wrapper=new QueryWrapper<>();
        Page<Setmeal> page=new Page<>(pagenum,pagesize);
        if (pageQuery!=null&&pageQuery.length()>0){
            wrapper.like("code",pageQuery).or().like("name",pageQuery).or().like("helpCode",pageQuery);
            page=this.page(page,wrapper);
        }else {
            page=this.page(page,null);
        }
        return page;
    }

    @Override
    public Setmeal findById(int id) {
        return setmealMapper.findById(id);
    }

    public void setSetmealAndCheckgroup(Integer setmealId,Integer[] checkgroupIds){
        if(checkgroupIds!=null&&checkgroupIds.length>0){
            for(Integer checkgroupId:checkgroupIds){
                Map<String,Integer> map=new HashMap<>();
                map.put("setmealId",setmealId);
                map.put("checkgroupId",checkgroupId);
                setmealMapper.setSetmealAndCheckGroup(map);
            }
        }
    }
}
